/******************************************************************************
 * @file     aw99703 i2c driver, control backlight of mipi lcd
 * @brief    header file for dpu driver
 ******************************************************************************/

#ifndef _DRV_AW99703_H_
#define _DRV_AW99703_H_

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef BIT
#define BIT(n)                  (1UL << (n))
#endif

#define AW99703_IIC_ADDR            0x36

/********************************************
 * Register List
 *******************************************/
#define AW99703_REG_ID				0x00
#define AW99703_REG_SFTRST			0x01
#define AW99703_REG_MODE			0x02
#define AW99703_REG_LEDCUR			0x03
#define AW99703_REG_BSTCTR1			0x04
#define AW99703_REG_BSTCTR2			0x05
#define AW99703_REG_LEDLSB			0x06
#define AW99703_REG_LEDMSB			0x07
#define AW99703_REG_PWM				0x08
#define AW99703_REG_TURNCFG			0x09
#define AW99703_REG_TRANCFG			0x0a
#define AW99703_REG_FLASH			0x0b
#define AW99703_REG_AFHIGH			0x0c
#define AW99703_REG_AFLOW			0x0d
#define AW99703_REG_FLAGS1			0x0e
#define AW99703_REG_FLAGS2			0x0f
    

/******************************************************
* Register Detail
*****************************************************/
/*SFTRST:0x01*/
#define AW99703_SFTRST_MASK			(~(1<<0))
#define AW99703_SFTRST_NOT_RST			(0<<0)
#define AW99703_SFTRST_RST			(1<<0)
    
/*MODE:0x02*/
#define AW99703_MODE_PDIS_MASK			(~(1<<4))
#define AW99703_MODE_PDIS_ENABLE		(0<<4)
#define AW99703_MODE_PDIS_DISABLE		(1<<4)
#define AW99703_MODE_MAP_MASK			(~(1<<2))
#define AW99703_MODE_MAP_EXPONENTIAL		(0<<2)
#define AW99703_MODE_MAP_LINEAR			(1<<2)
#define AW99703_MODE_WORKMODE_MASK		(~(3<<0))
#define AW99703_MODE_WORKMODE_STANDBY		(0<<0)
#define AW99703_MODE_WORKMODE_BACKLIGHT		(1<<0)
#define AW99703_MODE_WORKMODE_FLASH		(2<<0)
    
/*MODE:0x03*/
#define AW99703_LEDCUR_BLFS_MASK		(~(31<<3))
#define AW99703_LEDCUR_BLFS_SHIFT		(3)
#define AW99703_LEDCUR_CHANNEL_MASK		(~(7<<0))
#define AW99703_LEDCUR_CH3_ENABLE		(1<<2)
#define AW99703_LEDCUR_CH3_DISABLE		(0<<2)
#define AW99703_LEDCUR_CH2_ENABLE		(1<<1)
#define AW99703_LEDCUR_CH2_DISABLE		(0<<1)
#define AW99703_LEDCUR_CH1_ENABLE		(1<<0)
#define AW99703_LEDCUR_CH1_DISABLE		(0<<0)
    
    /*BSTCTR1:0x04*/
#define AW99703_BSTCTR1_SF_SFT_MASK		(~(3<<6))
#define AW99703_BSTCTR1_SF_SFT_UP20		(1<<6)
#define AW99703_BSTCTR1_SF_SFT_DOWN12		(2<<6)
#define AW99703_BSTCTR1_SF_SFT_DOWN24		(3<<6)
#define AW99703_BSTCTR1_SF_MASK			(~(1<<5))
#define AW99703_BSTCTR1_SF_500KHZ		(0<<5)
#define AW99703_BSTCTR1_SF_1000KHZ		(1<<5)
#define AW99703_BSTCTR1_OVPSEL_MASK		(~(7<<2))
#define AW99703_BSTCTR1_OVPSEL_17V		(0<<2)
#define AW99703_BSTCTR1_OVPSEL_24V		(1<<2)
#define AW99703_BSTCTR1_OVPSEL_31V		(2<<2)
#define AW99703_BSTCTR1_OVPSEL_38V		(3<<2)
#define AW99703_BSTCTR1_OVPSEL_41P5V		(4<<2)
#define AW99703_BSTCTR1_OCPSEL_MASK		(~(3<<0))
#define AW99703_BSTCTR1_OCPSEL_0P7A		(0<<0)
#define AW99703_BSTCTR1_OCPSEL_1P6A		(1<<0)
#define AW99703_BSTCTR1_OCPSEL_2P46A		(2<<0)
#define AW99703_BSTCTR1_OCPSEL_3P3A		(3<<0)
    
    /*BSTCTR2:0x05*/
#define AW99703_BSTCTR2_AFEN_MASK		(~(1<<7))
#define AW99703_BSTCTR2_AFEN_ENABLE		(1<<7)
#define AW99703_BSTCTR2_AFEN_DISABLE		(0<<7)
#define AW99703_BSTCTR2_IDCTSEL_MASK		(~(1<<6))
#define AW99703_BSTCTR2_IDCTSEL_4P7UH		(0<<6)
#define AW99703_BSTCTR2_IDCTSEL_10UH		(1<<6)
#define AW99703_BSTCTR2_EMISEL_MASK		(~(7<<3))
#define AW99703_BSTCTR2_EMISEL_TYPICAL		(0<<3)
#define AW99703_BSTCTR2_EMISEL_SLOW1		(1<<3)
#define AW99703_BSTCTR2_EMISEL_SLOW2		(2<<3)
#define AW99703_BSTCTR2_EMISEL_SLOW3		(3<<3)
#define AW99703_BSTCTR2_EMISEL_FAST1		(4<<3)
#define AW99703_BSTCTR2_EMISEL_FAST2		(5<<3)
#define AW99703_BSTCTR2_EMISEL_FAST3		(6<<3)
#define AW99703_BSTCTR2_EMISEL_FAST4		(7<<3)
#define AW99703_BSTCTR2_ADEN_MASK		(~(1<<2))
#define AW99703_BSTCTR2_ADEN_ENABLE		(1<<2)
#define AW99703_BSTCTR2_ADEN_DISABLE		(0<<2)
    
    /*PWM:0x08*/
#define AW99703_PWM_P_SF_MASK			(~(3<<6))
#define AW99703_PWM_P_SF_800KHZ			(0<<6)
#define AW99703_PWM_P_SF_4MKHZ			(1<<6)
#define AW99703_PWM_P_SF_24MKHZ			(2<<6)
#define AW99703_PWM_P_ACT_MASK			(~(1<<5))
#define AW99703_PWM_P_ACT_HIGH			(0<<5)
#define AW99703_PWM_P_ACT_LOW			(1<<5)
#define AW99703_PWM_P_HYS_MASK			(~(7<<2))
#define AW99703_PWM_P_HYS_NONE			(0<<2)
#define AW99703_PWM_P_HYS_1LSB			(1<<2)
#define AW99703_PWM_P_HYS_2LSB			(2<<2)
#define AW99703_PWM_P_HYS_3LSB			(3<<2)
#define AW99703_PWM_P_HYS_4LSB			(4<<2)
#define AW99703_PWM_P_HYS_5LSB			(5<<2)
#define AW99703_PWM_P_HYS_6LSB			(6<<2)
#define AW99703_PWM_P_HYS_7LSB			(7<<2)
#define AW99703_PWM_P_FLT_MASK			(~(3<<0))
#define AW99703_PWM_P_FLT_NONE			(0<<0)
#define AW99703_PWM_P_FLT_100MS			(1<<0)
#define AW99703_PWM_P_FLT_150MS			(2<<0)
#define AW99703_PWM_P_FLT_200MS			(3<<0)
    
    /*TURNCFG:0x09*/
#define AW99703_TURNCFG_ON_TIM_MASK		(~(15<<4))
#define AW99703_TURNCFG_OFF_TIM_MASK		(~(15<<0))
    
    /*TRANCFG:0x0A*/
#define AW99703_TRANCFG_PWM_TIM_MASK		(~(7<<4))
#define AW99703_TRANCFG_I2C_TIM_MASK		(~(15<<0))
    
#define MAX_BRIGHTNESS		(2047)
#define AW_READ_CHIPID_RETRIES 5
#define AW_READ_CHIPID_RETRY_DELAY 2




/**
  \brief       init aw99703
  \param[in]   backlight_code, 0-2047.
  \param[in]   en_ch_msk, channel mask, there are 3 channels.
  \return      error code
*/
#define AW99703_MAX_BL_CODE (2047)
#define AW99703_BL_CH1      BIT(0)
#define AW99703_BL_CH2      BIT(1)
#define AW99703_BL_CH3      BIT(2)
int aw99703_init(uint16_t backlight_code, uint8_t bl_ch_msk);

/**
  \brief       adjust back light
  \param[in]   backlight_code, 0-2047, 0 turn off, 2047 max brightness
  \return      error code
*/
int aw99703_adj_bklgt(int backlight_code);


#ifdef __cplusplus
}
#endif

#endif /* _DRV_DPU_H_ */

